import api from '@/api';
import store from '@/store';
import { nameObj } from '@/config/dic';

/**
 * 对参数做一层转换，如{优先级：xx}=>{level:xx}
 * @param obj 
 * @returns 
 */
const nameToKeyObj = (obj) => {
    // console.log(obj);
    const newObj = {};
    Object.keys(obj).forEach((key) => {
        newObj[nameObj[key]] = obj[key];
    });
    return newObj;
};

// 获取选项
const getOptions = async () => {
    const query = [
        '优先级',
        '需求类型',
        '产品需求类型',
        '状态',
        '产品需求状态',
        '阶段',
        '分析方式',
        '流程差异性',
        '系统类型',
        '流程状态',
        '版本状态',
        '所属端',
        '变更类型',
        '重要级别',
        '变更流程状态',
        '补录流程状态',
        '补录数据类型',
        '域名地址',
        '能效阶段',
        '系统模块',
        '供应商状态',
        '数据来源'
    ];
    const { success, moule } = await api.dicModel.findBySources(query);
    return success ? { ...nameToKeyObj(moule) } : {};
};
// 特殊处理的选项
const getSpecialOptions = async () => {
    const query = ['产品需求阶段', '岗位']; // '来源', 
    const { success, moule } = await api.dicModel.pmpDictionaryDTOs(query);
    return success ? { ...nameToKeyObj(moule) } : {};
};
// 获取所有系统
const getAllSystem = async () => {
    const { success, moule } = await api.productDetailModel.findAll();
    if (success) {
        const list = moule.map((item) => ({
            ...item,
            code: item.id.toString()
        }));
        return { fromSystem: [...list] };
    } else {
        return { fromSystem: [] };
    }
};
// 获取我负责的系统
const getMySystem = async () => {
    const { success, moule } = await api.systemManagerModel.findBySystemAndEmpId();
    if (success) {
        const list = moule.map((item) => ({
            ...item,
            system: item.systemName,
            code: item.id.toString()
        }));
        return { mySystem: [...list] };
    } else {
        return { mySystem: [] };
    }
};
// 获取所有选项
export const getAllOptions = async () => {
    let options: any = {};
    const res = await Promise.all([getOptions(), getSpecialOptions(), getAllSystem(), getMySystem()]);
    res.forEach((item) => {
        options = { ...options, ...item };
    });
    console.log(options);
    // 版本详情特殊处理
    if (options.systemType) {
        const processType = options.systemType.filter((item) => item.code !== '3');
        options = { ...options, processType };
    }
    // 阶段特殊处理
    if (options.stage) {
        const stage = options.stage.filter((item) => item.code !== '3');
        options = { ...options, stage };
    }
    return {
        success: true,
        moule: options
    };
};

// 所有字典值回显方法
export const valToNameFilter = (field: string, val: any) => {
    if (!val && val !== 0) {
        return;
    }
    const dicOptions = store.getters.dicOptions;
    if (dicOptions[field]) {
        if (field === 'fromSystem' && typeof val === 'string' && val.indexOf(',') > 0) {
            const arr = val.split(',');
            const targetArr = dicOptions[field].filter((item) => arr.includes(item.code));
            return targetArr.map((item) => item.systemName).join(',');
        } else {
            const target = dicOptions[field].find((item) => item.code === val || item.id === val || item.catalogCode === val);
            if (target) {
                return target.dimensionName || target.systemName;
            }
        }
    }
};

